package com.symone.webcms.controller;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.symone.webcms.controller.base.BaseController;
import com.symone.webcms.model.MenuAction;
import com.symone.webcms.model.base.ResultMessage;
import com.symone.webcms.service.MenuActionService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import com.baomidou.mybatisplus.plugins.Page;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import io.swagger.annotations.*;

/**
 *   @description : 菜单行为管理
 *   ---------------------------------
 * 	 @author Archer Chu
 *   @since 2019-01-02
 */
@RestController
@Api(value="/MenuAction", description="菜单行为 管理")
@RequestMapping("/api/action")
public class MenuActionController extends BaseController {

    private final Logger logger = LoggerFactory.getLogger(MenuActionController.class);

    @Autowired
    public MenuActionService menuActionService;

    /**
     * @description : 获取菜单行为分页列表
     * ---------------------------------
     * @author : Archer Chu
     * @since : Create in 2019-01-02
     */
    @RequiresPermissions("dev:menu:action:page")
    @RequestMapping(value = "/page", method = RequestMethod.GET)
    public ResultMessage page( Integer pageSize, Integer pageNum) {
        Wrapper<MenuAction> ew = new EntityWrapper<MenuAction>();
        ew.where("deleted=0");

        //logger.debug("SQL:"+ew.originalSql());

        Page<MenuAction> page = new Page<MenuAction>(Integer.valueOf(pageNum),Integer.valueOf(pageSize));
        menuActionService.findPage(page, ew);

        return ResultMessage.success("获取菜单行为的分页列表",page);
    }

    /**
     * @description : 获取所有菜单行为记录
     * ---------------------------------
     * @author : Archer Chu
     * @since : Create in 2019-01-02
     */
    @RequiresPermissions("dev:menu:action:list")
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public ResultMessage list() {
        return ResultMessage.success("获取所有菜单行为信息",menuActionService.findAll());
    }

    /**
     * @description : 通过id获取菜单行为
     * ---------------------------------
     * @author : Archer Chu
     * @since : Create in 2019-01-02
     */
    @RequiresPermissions("dev:menu:action:get")
    @RequestMapping(value = "/get", method = RequestMethod.GET)
    public ResultMessage get(Long id) {
        return ResultMessage.success("获取菜单行为信息成功!",menuActionService.selectById(id));
    }

    /**
     * @description : 添加菜单行为
     * ---------------------------------
     * @author : Archer Chu
     * @since : Create in 2019-01-02
     */
    @RequiresPermissions("dev:menu:action:add")
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public ResultMessage add(MenuAction menuAction){
        menuActionService.insert(menuAction);
        return ResultMessage.success("添加菜单行为成功!");
    }

    /**
     * @description : 通过id逻辑删除菜单行为
     * ---------------------------------
     * @author : Archer Chu
     * @since : Create in 2019-01-02
     */
    @RequiresPermissions("dev:menu:action:remove_flag")
    @RequestMapping(value = "/removeFlag", method = RequestMethod.PUT)
    public ResultMessage removeFlag(Long id){
        if(menuActionService.deleteFlagById(id)) {
            return ResultMessage.success("逻辑菜单行为删除成功！");
        } else {
            return ResultMessage.fail("逻辑菜单行为删除失败！");
        }
    }

    /**
     * @description : 通过id逻辑删除菜单行为
     * ---------------------------------
     * @author : Archer Chu
     * @since : Create in 2019-01-02
     */
    @RequiresPermissions("dev:menu:action:remove")
    @RequestMapping(value = "/remove", method = RequestMethod.DELETE)
    public ResultMessage remove(long id){
        if(menuActionService.deleteById(id)) {
            return ResultMessage.success("物理删除菜单行为成功！");
        } else {
            return ResultMessage.fail("物理删除菜单行为失败！");
        }
    }

    /**
     * @description : 更新菜单行为信息
     * ---------------------------------
     * @author : Archer Chu
     * @since : Create in 2019-01-02
     */
    @RequiresPermissions("dev:menu:action:update")
    @RequestMapping(value = "/update", method = RequestMethod.PUT)
    public ResultMessage update(MenuAction menuAction) {
        if(menuActionService.updateById(menuAction)) {
            return ResultMessage.success("菜单行为信息更新成功!");
        }else{
            return ResultMessage.fail("菜单行为信息更新失败!");
        }
    }

    /**
    * @description : 清除菜单行为记录
    * ---------------------------------
     * @author : Archer Chu
     * @since : Create in 2019-01-02
    */
    @RequiresPermissions("dev:menu:action:clear")
    @RequestMapping(value = "/clear", method = RequestMethod.DELETE)
    public ResultMessage clear(){
        int num = menuActionService.clear();
        if(num > 0) {
            return ResultMessage.success("已经清理了 "+ num +" 条记录！");
        } else {
            return ResultMessage.fail("没有菜单行为记录被清除！");
        }
    }

}
